<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8" />
    <title>智慧社区-版本管理</title>

    <meta name="description" content="Dashboard" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/resource/assets/img/favicon.png" type="image/x-icon">

    <!--Basic Styles-->
    <link href="${pageContext.request.contextPath}/resource/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link href="${pageContext.request.contextPath}/resource/assets/css/font-awesome.min.css" rel="stylesheet" />

    <!--Beyond styles-->
    <link href="${pageContext.request.contextPath}/resource/assets/css/beyond.min.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath}/resource/assets/css/animate.min.css" rel="stylesheet" />
<script src="${pageContext.request.contextPath}/resource/assets/js/skins.min.js"></script>
</head>
<body>
<%@include file="../../header.jsp" %>
	<!-- Main Container -->
    <div class="main-container container-fluid">
        <!-- Page Container -->
        <div class="page-container">
            <!-- Page Sidebar -->
            <%@include file="../nav.jsp" %><!-- /Page Sidebar -->
            <!-- Page Content -->
            <div class="page-content">
                <!-- Page Breadcrumb -->
                <div class="page-breadcrumbs">
                    <ul class="breadcrumb" style="margin-top: 8px;">
                        <li>
                            <i class="fa fa-home"></i>
                            <a href="#">首页</a>
                        </li>
                        <li><a href="#">版本管理</a></li>
                        <li class="active">客户端更新</li>
                    </ul>
                </div><!-- /Page Breadcrumb -->
                <!-- Page Header -->
                <div class="page-header position-relative">
                    <div class="header-title" style="margin-top: 2px;">
                        <h1>
                            客户端更新
                        </h1>
                    </div><!--Header Buttons-->
                    <div class="header-buttons">
                        <a class="sidebar-toggler" href="#">
                            <i class="fa fa-arrows-h"></i>
                        </a>
                        <a class="refresh" id="refresh-toggler" href="">
                            <i class="glyphicon glyphicon-refresh"></i>
                        </a>
                        <a class="fullscreen" id="fullscreen-toggler" href="#">
                            <i class="glyphicon glyphicon-fullscreen"></i>
                        </a>
                    </div><!--Header Buttons End-->
                </div><!-- /Page Header -->
                <!-- Page Body -->
                <div class="page-body">
                	<div class="row">
	                	<div class="col-lg-6 col-sm-6 col-xs-12">
	                		<div class="widget">
                                        <div class="widget-header bordered-bottom bordered-palegreen">
                                            <span class="widget-caption">上传安装包</span>
                                        </div>
                                        <div class="widget-body">
                                           <div id="registration-form">
                                                        <form role="form">
                                                            <div class="form-title">
                                                            	安装包信息
                                                            </div>
                                                            	<div class="form-group">
                                                                        <select id="village" style="width:100%;">
                                                                        <c:forEach var="village" items="${villages }">
												                      		<option value="${village.objectId }">${village.name }</option>
												                      	</c:forEach>
												                      	<option value="0">所有</option>
                                            						</select>
                                                              	</div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="versionCode" type="text" class="form-control"  placeholder="版本号">
                                                                    <i class="glyphicon glyphicon-user circular"></i>
                                                                </span>
                                                            </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="versionName" type="text" class="form-control"  placeholder="版本名称">
                                                                    <i class="glyphicon glyphicon-user circular"></i>
                                                                </span>
                                                            </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                	<textarea class="form-control" rows="10" id="content" placeholder="更新描述"></textarea>
                                                                     <i class="glyphicon glyphicon-briefcase darkorange"></i>
                                                         		</span>
                                                            </div>
                                                            
                                                            <div class="form-group">
										                      <label for="fileUpload">上传安装包</label>
										                      <input type="file" id="fileUpload" name="upload">
										                      <p class="help-block">安装包较大，上传时间可能较长，请耐心等待.</p>
                    										</div>
                                                            <hr class="wide" />
                                                            <div class="row" style="text-align: right;margin-right: 10px;">
                                                            	<button type="button" class="btn btn-blue" onclick="save()"> 确定 </button>
                                                            </div>
                                                            
                                                        </form>
                                                    </div> 
                                        </div>
                                    </div>
	                	</div>
                	
                    </div>
                </div><!-- /Page Body -->
             </div><!-- /Page Content -->
     	</div><!-- /Page Container -->
     </div><!-- /Main Container -->
     	<script src="${pageContext.request.contextPath}/resource/assets/js/jquery-2.0.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/resource/assets/js/bootstrap.min.js"></script>

    <!--Beyond Scripts-->
    <script src="${pageContext.request.contextPath}/resource/assets/js/beyond.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/resource/assets/js/ajaxfileupload.js"></script>
    <script src="${pageContext.request.contextPath}/resource/assets/js/datetime/moment.js"></script>
    <script src="${pageContext.request.contextPath}/resource/assets/js/datetime/daterangepicker.js"></script>
    <script src="${pageContext.request.contextPath}/resource/assets/js/select2/select2.js"></script>
    <script type="text/javascript">
    	$('.loading-container').hide();
    	$('#menu_version').addClass("active open");
    	$('#menu_update_client').addClass("active");
    	$("#village").select2();
    	
    	function save(){
    		
    		var versionCode = $('#versionCode').val();
    		var versionName = $('#versionName').val();
    		var content = $('#content').val();
    		var vid = $('#village').val();
    		if ($.trim(versionCode) == "" || $.trim(content) == "") {
    			alert("请完善安装包信息后再提交保存");
    			return;
    		}
    		
    		$('.loading-container').show();
    		$.ajaxFileUpload({
		        url: '${pageContext.request.contextPath}/version_updateClient',
		        secureuri: false,
		        data: {vid:vid,versionCode:versionCode,content:content,versionName:versionName},
		        fileElementId: 'fileUpload',
		        dataType: 'json',
		        success: function (data) {
		        	$('.loading-container').hide();
		        	var reg = /<pre.+?>(.+)<\/pre>/g;  
		        	data.match(reg);  
		        	data = RegExp.$1;
		        	if (data == 201){
						alert("鉴权已过期，请重新登录");
						location.href="/WisdomHome";
						return;
					}
		        	
		        	if(data == 203){
		        		alert("请上传有效的apk安装包");
		        		return;
		        	}
		        	if(data == 200){
		        		alert("安装包上传成功，客户端将收到更新通知");
		        		$('#versionCode').val("");
			        	$('#content').val("");
		        	}
		        },
		        error: function (data) {
		        	alert("error msg:"+data.responseText);
		        }
		    });
    	}
    </script>
</body>
</html>